package com.shopping.service.impl;

import com.shopping.bean.Collect;
import com.shopping.dao.CollectDao;
import com.shopping.service.ICollectService;
import org.bouncycastle.asn1.cmp.CAKeyUpdAnnContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CollectService implements ICollectService {
@Autowired
    CollectDao collectDao;
    @Override
    public boolean addCollect(int userid, int productid) {
        if (isProductCollect(userid, productid)) {
            return false; // 商品已收藏
        }
        Collect collect = new Collect();
        collect.setUserid(userid);
        collect.setProductid(productid);
        collect.setCollecttime(new Date());
        collectDao.addCollect(collect);
        return true; // 收藏成功
    }

    @Override
    public boolean isProductCollect(int userid, int productid) {
        Map<String, Object> params = new HashMap<>();
        params.put("userid", userid);
        params.put("productid", productid);
        int count = collectDao.isProductCollect(params);
        return count > 0;
    }

    @Override
    public List<Collect> getCollectListByUserId(int userId) {
        return collectDao.getCollectListByUserId(userId);
    }

    @Override
    public boolean deleteCollect(int userId, int productId) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("productId", productId);
        int result = collectDao.deleteCollect(params);
        return result > 0;
    }

    @Override
    public boolean deleteCollectById(int collectid) {
        int result = collectDao.deleteCollectById(collectid);
        return result > 0;
    }

    @Override
    public int getCollectCountByUserId(int userId) {
        return collectDao.getCollectCountByUserId(userId);
    }
}
